<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_quota_rule".
 *
 * @property int $qid
 * @property int $tid
 * @property string $place_sta_time
 * @property string $place_end_time
 * @property int $min_sta_order
 * @property int $max_end_order
 * @property int $max_order_num
 * @property int $phone_day
 * @property int $bug_ticket_num
 * @property int $phone_day_num
 * @property int $advance_num
 * @property string $online_time
 * @property string $offline_time
 * @property string $day_end_time
 * @property string $is_oragin
 * @property int $age_rule_type
 * @property string $create_time
 */
class TicketQuotaRule extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_quota_rule';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['tid','required','message' => '门票id不能为空'],
            ['tid','integer','message' => '门票id类型有误'],
            ['min_sta_order','integer','message' => '最小起订数必须是数字'],
            ['min_sta_order','compare','compareValue' => 0,'operator' => '>','message' => '最小起订数必须大于0'],
            ['max_end_order','requiredByMaxOrder'],
            ['advance_num','compare','compareValue' => 0,'operator' => '>=','message' => '提前预定天数必须大于0'],
            ['offline_time','requiredByOffline'],
            ['phone_day_num','compare','compareValue' => 0,'operator' => '>','message' => '游玩当天票数必须大于0'],
            ['place_sta_time','requiredByPlaceTime'],
            [[ 'min_sta_order', 'max_end_order', 'max_order_num', 'phone_day', 'bug_ticket_num', 'phone_day_num', 'advance_num', 'age_rule_type'], 'integer'],
            [['place_sta_time', 'place_end_time', 'online_time', 'offline_time', 'day_end_time', 'create_time'], 'safe'],
            [['is_oragin'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'qid' => 'Qid',
            'tid' => 'Tid',
            'place_sta_time' => 'Place Sta Time',
            'place_end_time' => 'Place End Time',
            'min_sta_order' => 'Min Sta Order',
            'max_end_order' => 'Max End Order',
            'max_order_num' => 'Max Order Num',
            'phone_day' => 'Phone Day',
            'bug_ticket_num' => 'Bug Ticket Num',
            'phone_day_num' => 'Phone Day Num',
            'advance_num' => 'Advance Num',
            'online_time' => 'Online Time',
            'offline_time' => 'Offline Time',
            'day_end_time' => 'Day End Time',
            'is_oragin' => 'Is Oragin',
            'age_rule_type' => 'Age Rule Type',
            'create_time' => 'Create Time',
        ];
    }


    public function addQuotaRule($params)
    {
        $data = $this->setData($params);
        if ($this->load($data) and $this->validate()){
            $model = self::find()->where('tid = :id',[':id' => $data['TicketQuotaRule']['tid']])->one();
            if (!is_null($model)){
                $model->place_sta_time = $data['TicketQuotaRule']['place_sta_time'];
                $model->place_end_time = $data['TicketQuotaRule']['place_end_time'];
                $model->min_sta_order = $data['TicketQuotaRule']['min_sta_order'];
                $model->max_end_order = $data['TicketQuotaRule']['max_end_order'];
                $model->max_order_num = $data['TicketQuotaRule']['max_order_num'];
                $model->phone_day = $data['TicketQuotaRule']['phone_day'];
                $model->bug_ticket_num = $data['TicketQuotaRule']['bug_ticket_num'];
                $model->phone_day_num = $data['TicketQuotaRule']['phone_day_num'];
                $model->online_time = $data['TicketQuotaRule']['online_time'];
                $model->offline_time = $data['TicketQuotaRule']['offline_time'];
                $model->day_end_time = $data['TicketQuotaRule']['day_end_time'];
                $model->is_oragin = $data['TicketQuotaRule']['is_oragin'];
                $model->advance_num = $data['TicketQuotaRule']['advance_num'];
                $model->age_rule_type = $data['TicketQuotaRule']['age_rule_type'];
                if ($model->save(0)){
                    return true;
                }
            }else{
                if ($this->save(0)){
                    return true;
                }
            }

        }
        return false;
    }


    private function setData($params)
    {
        if (isset($params['tid']) and !empty($params['tid'])){
            $data['TicketQuotaRule']['tid'] = $params['tid'];
        }else{
            $data['TicketQuotaRule']['tid'] = '';
        }

        if (isset($params['place_sta_time']) and !empty($params['place_sta_time'])){
            $data['TicketQuotaRule']['place_sta_time'] = $params['place_sta_time'];
        }else{
            $data['TicketQuotaRule']['place_sta_time'] = '0000-00-00 00:00:00';
        }
        if (isset($params['place_end_time']) and !empty($params['place_end_time'])){
            $data['TicketQuotaRule']['place_end_time'] = $params['place_end_time'];
        }else{
            $data['TicketQuotaRule']['place_end_time'] = '0000-00-00 00:00:00';
        }
        if (isset($params['min_sta_order']) and !empty($params['min_sta_order'])){
            $data['TicketQuotaRule']['min_sta_order'] = $params['min_sta_order'];
        }else{
            $data['TicketQuotaRule']['min_sta_order'] = 1;
        }
        if (isset($params['max_end_order']) and !empty($params['max_end_order'])){
            $data['TicketQuotaRule']['max_end_order'] = $params['max_end_order'];
        }else{
            $data['TicketQuotaRule']['max_end_order'] = 0;
        }
//        var_dump($data);die;
        if (isset($params['max_order_num']) and !empty($params['max_order_num'])){
            $data['TicketQuotaRule']['max_order_num'] = $params['max_order_num'];
        }else{
            $data['TicketQuotaRule']['max_order_num'] = 0;
        }
        if (isset($params['phone_day']) and !empty($params['phone_day'])){
            $data['TicketQuotaRule']['phone_day'] = $params['phone_day'];
        }else{
            $data['TicketQuotaRule']['phone_day'] = 0;
        }
        if (isset($params['bug_ticket_num']) and !empty($params['bug_ticket_num'])){
            $data['TicketQuotaRule']['bug_ticket_num'] = $params['bug_ticket_num'];
        }else{
            $data['TicketQuotaRule']['bug_ticket_num'] = 0;
        }
        if (isset($params['phone_day_num']) and !empty($params['phone_day_num'])){
            $data['TicketQuotaRule']['phone_day_num'] = $params['phone_day_num'];
        }else{
            $data['TicketQuotaRule']['phone_day_num'] = 1;
        }
        if (isset($params['advance_num']) and !empty($params['advance_num'])){
            $data['TicketQuotaRule']['advance_num'] = $params['advance_num'];
        }else{
            $data['TicketQuotaRule']['advance_num'] = 0;

        }
        if (isset($params['online_time']) and !empty($params['online_time'])){
            $data['TicketQuotaRule']['online_time'] = $params['online_time'];
        }else{
            $data['TicketQuotaRule']['online_time'] = '0000-00-00 00:00:00';
        }
        if (isset($params['offline_time']) and !empty($params['offline_time'])){
            $data['TicketQuotaRule']['offline_time'] = $params['offline_time'];
        }else{
            $data['TicketQuotaRule']['offline_time'] = '0000-00-00 00:00:00';
        }

        if (isset($params['day_end_time']) and !empty($params['day_end_time'])){
            $data['TicketQuotaRule']['day_end_time'] = '0000-00-00 '.$params['day_end_time'];
        }else{
            $data['TicketQuotaRule']['day_end_time'] = '0000-00-00 00:00:00';
        }

        if (isset($params['is_oragin']) and !empty($params['is_oragin'])){
            $data['TicketQuotaRule']['is_oragin'] = $params['is_oragin'];
        }else{
            $data['TicketQuotaRule']['is_oragin'] = '2';
        }
        if (isset($params['age_rule_type']) and !empty($params['age_rule_type'])){
            $data['TicketQuotaRule']['age_rule_type'] = $params['age_rule_type'];
        }else{
            $data['TicketQuotaRule']['age_rule_type'] = '1';
        }

        return $data;
    }


    /*** 获取门票限购信息
     * @param $tid
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getQuotaInfo($tid)
    {
        $data = self::find()->where('tid = :id',[':id' => $tid])->asArray()->one();
        return $data;
    }

    /*** 验证最大起订数必须大于最小起订数
     * @param $attribute
     * @param $params
     */
    public function requiredByMaxOrder($attribute,$params)
    {
        if ($this->min_sta_order > 0 and $this->max_end_order != 0){
            if ($this->max_end_order < $this->min_sta_order){
                $this->addError($attribute,"最大起订数必须大于最小起订数");
            }
        }
    }

    public function requiredByOffline($attribute,$params)
    {
        if  ($this->online_time != ''){
            if ($this->offline_time < $this->online_time){
                $this->addError($attribute,"下线时间必须大于上线时间");
            }
        }
    }

    public function requiredByPlaceTime($attribute,$params)
    {
        if ($this->place_sta_time != ''){
            if ($this->place_sta_time > $this->place_end_time){
                $this->addError($attribute,"下单结束时间必须大于下单开始时间");
            }
        }
    }


}
